package com.jutixueyuan.service.impl;

import com.jutixueyuan.pojo.House;
import com.jutixueyuan.pojo.LivegoodsResult;
import com.jutixueyuan.service.DetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DetailsServiceImpl implements DetailsService {


    @Value("${livegoods.fastdfs.nginx}")
    private String livegoodsFastdfsNginx;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Cacheable(cacheNames="com.jutixueyuan",key = "'findById'+#id")
    public House findById(String id) {

        try {
            House house = mongoTemplate.findById(id, House.class);
            List<String> imgs = house.getImgs();
            for (int i = 0; i < imgs.size(); i++) {
                house.getImgs().set(i,livegoodsFastdfsNginx+imgs.get(i));
            }
            System.out.println("从数据库查询，没有走缓存");
            //LivegoodsResult result = LivegoodsResult.ok(house);
            return house;
        } catch (Exception e) {
            return null;
        }
    }
}
